package com.ytjj.qmyx.mall.service.impl;

import com.alibaba.nacos.common.utils.Md5Utils;
import com.ytjj.qmyx.mall.mapper.AdvertCallbackMapper;
import com.ytjj.qmyx.mall.mapper.AdvertReportMapper;
import com.ytjj.qmyx.mall.model.AdvertCallback;
import com.ytjj.qmyx.mall.model.AdvertCallbackExample;
import com.ytjj.qmyx.mall.model.AdvertReport;
import com.ytjj.qmyx.mall.model.AdvertReportExample;
import com.ytjj.qmyx.mall.service.AdvertReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdvertReportServiceImpl implements AdvertReportService {

    @Resource
    private AdvertReportMapper advertReportMapper;

    @Resource
    private AdvertCallbackMapper advertCallbackMapper;

    @Override
    public void saveAdvertReport(AdvertReport advertReport) {
        if (advertReport.getChannel().contains("ytjj-ks")) {
            advertReport.setAdvertPlatform("KS");
        }else if(advertReport.getChannel().contains("ytjj-tt") || advertReport.getChannel().contains("ytjj-dy")){
            advertReport.setAdvertPlatform("TT");
        }else{
            advertReport.setAdvertPlatform("OTHER");
        }
        advertReport.setCreateTime(new Date());
        if("ios".equals(advertReport.getPlatform())) {
            advertReport.setIdfa(advertReport.getImei());
        }
        AdvertCallback advertCallback=getAdvertCallBack(advertReport.getAdvertPlatform(),advertReport.getType(),advertReport.getPlatform(),advertReport.getImei(),advertReport.getOaid(),advertReport.getIdfa());
        if(null==advertCallback){//不存在callback数据
            return;
        }
        advertReport.setCallback(advertCallback.getCallback());
        advertReport.setUa(advertCallback.getUa());
        advertReportMapper.insert(advertReport);
    }

    @Override
    public AdvertReport getAdvertReport(AdvertReport advertReport) {
        AdvertReportExample advertReportExample=new AdvertReportExample();
        AdvertReportExample.Criteria criteria=advertReportExample.createCriteria();
        criteria.andTypeEqualTo(advertReport.getType());
        criteria.andAdvertPlatformEqualTo(advertReport.getAdvertPlatform());
        if("android".equals(advertReport.getPlatform())) {
            criteria.andPlatformEqualTo("android");
            criteria.andImeiEqualTo(advertReport.getImei());
        }else if("ios".equals(advertReport.getPlatform())) {
            criteria.andPlatformEqualTo("ios");
            criteria.andIdfaEqualTo(advertReport.getIdfa());
        }else{
            return null;
        }
        List<AdvertReport> advertReportList=advertReportMapper.selectByExample(advertReportExample);
        if(advertReportList!=null&&advertReportList.size()>0){
            return advertReportList.get(0);
        }
        return null;
    }

    private AdvertCallback getAdvertCallBack(String advertPlatform,String type,String platform,String imei,String oaid,String idfa) {
        AdvertCallbackExample advertCallbackExample=new AdvertCallbackExample();
        AdvertCallbackExample.Criteria criteria=advertCallbackExample.createCriteria();
        criteria.andAdvertPlatformEqualTo(advertPlatform);
        criteria.andTypeEqualTo(type);
        if("android".equals(platform)) {
            if(StringUtils.isNotBlank(imei)){
                criteria.andImeiEqualTo(Md5Utils.getMD5(imei,"UTF-8").toLowerCase());
            }else{
                criteria.andOaidEqualTo(oaid);
            }
        }else if("ios".equals(platform)) {
            criteria.andIdfaEqualTo(idfa);
        }else{
            return null;
        }

        List<AdvertCallback> advertCallbackList=advertCallbackMapper.selectByExample(advertCallbackExample);
        if(null!=advertCallbackList&&advertCallbackList.size()>0){
            return advertCallbackList.get(0);
        }
        return null;
    }
}
